#include <stdio.h>
#include <string.h>
#include "CLComValue.h"

void CLComValue::Init()
{
    m_valuestr = "";
    m_keystr = "";
    m_pObj = nullptr;
    m_llObjLen = 0;

    SetDType(ECLCOMVALUE_TYPE_NO);
}
CLComValue::~CLComValue()
{
    if (m_pObj)
    {
        delete[] m_pObj;
        m_pObj = nullptr;
    }
}
CLComValue::CLComValue()
{
	Init();
}
CLComValue::CLComValue(const char* key)
{
	Init();
    m_keystr = key;
}
void CLComValue::CopyValue(const CLComValue& other, bool bobjDeep)
{
    m_keystr = other.m_keystr;
    m_valuestr = other.m_valuestr;
	m_eDType = other.m_eDType;

	m_llObjLen = other.m_llObjLen;
	if (bobjDeep)
	{
		m_pObj = new char[m_llObjLen];
		memcpy((void*)m_pObj, (void*)other.m_pObj,
			m_llObjLen);
	}
	else
	{
		m_pObj = other.m_pObj;
	}
}
CLComValue::CLComValue(const CLComValue& other)
{
	CopyValue(other, false);
}
void CLComValue::DeepCopy(const CLComValue& other)
{
	CopyValue(other, true);
}
CLComValue& CLComValue::operator=(const CLComValue& other)
{
	CopyValue(other, false);
	return *this;
}
void CLComValue::SetIntValue(long long llValue)
{
    m_valuestr = llValue;

    m_eDType = ECLCOMVALUE_TYPE_INT;
}
void CLComValue::SetDoubleValue(double dValue)
{
    m_valuestr = dValue;
    m_eDType = ECLCOMVALUE_TYPE_DOUBLE;
}
void CLComValue::SetStringValue(const char* strValue)
{
    m_valuestr = strValue;
    m_eDType = ECLCOMVALUE_TYPE_STRING;
}
void CLComValue::SetObjValue(void* pValue, long long nLen)
{
    m_pObj = (char*)pValue;

	m_llObjLen = nLen;
    m_eDType = ECLCOMVALUE_TYPE_POBJ;
}
void CLComValue::SetObjValueDeep(void* pValue, long long nLen)
{
	if (!pValue || !nLen)
	{
		m_pObj = NULL;
		m_llObjLen = 0;
		return;
	}
	m_llObjLen = nLen;
	m_pObj = new char[m_llObjLen];
	memcpy((void*)m_pObj, pValue,
		m_llObjLen);
    m_eDType = ECLCOMVALUE_TYPE_POBJ;
}
void CLComValue::SetKey(const char* key)
{
    m_keystr = key;
}
void CLComValue::SetDType(ECLCOMVALUE_TYPE dtype)
{
	m_eDType = dtype;
}
ECLCOMVALUE_TYPE CLComValue::GetDType()const
{
	return m_eDType;
}
const char* CLComValue::GetKey()const
{
    return m_keystr.GetChars();
}
long long CLComValue::GetIntValue()const
{
    return m_valuestr.GetBigInt();
}
double CLComValue::GetDoubleValue()const
{
    return m_valuestr.GetDouble();
}
const char* CLComValue::GetStringValue()const
{
    return m_valuestr.GetChars();
}
void* CLComValue::GetPObject()const
{
	return m_pObj;
}
long long CLComValue::GetPObjectLen()const
{
	return m_llObjLen;
}
bool CLComValue::FindAttributeDouble(
    map<string, CLComValue>& checkAttribute,
	string key,
	double& dValue)
{
    map<string, CLComValue>::iterator it =
		checkAttribute.find(key);
	if (it == checkAttribute.end())
	{
		return false;
	}
	dValue = it->second.GetDoubleValue();

	return true;
}

bool CLComValue::FindAttributeString(
    map<string, CLComValue>& checkAttribute,
	string key,
	string& strValue)
{
    map<string, CLComValue>::iterator
		it = checkAttribute.find(key);
	if (it == checkAttribute.end())
	{
		return false;
	}
	strValue = it->second.GetStringValue();

	return true;
}

bool CLComValue::FindAttributeBigInt(
    map<string, CLComValue>& checkAttribute,
	string key,
    long long& nValue)
{
    map<string, CLComValue>::iterator
		it = checkAttribute.find(key);
	if (it == checkAttribute.end())
	{
		return false;
	}
	nValue = it->second.GetIntValue();

	return true;
}

bool CLComValue::FindAttributeInt(
    map<string, CLComValue>& checkAttribute,
	string key,
	int& nValue)
{
    map<string, CLComValue>::iterator it = checkAttribute.find(key);
	if (it == checkAttribute.end())
	{
		return false;
	}

	nValue = it->second.GetIntValue();

	return true;
}

bool CLComValue::FindAttributeObject(
    map<string, CLComValue>& checkAttribute,
	string key,
	unsigned char*& pValue)
{
    map<string, CLComValue>::iterator
		it = checkAttribute.find(key);
	if (it == checkAttribute.end())
	{
		return false;
	}
	pValue = (unsigned char*)it->second.GetPObject();

	return true;
}
